//
//  CustomSlider.m
//  ijkPlayer
//
//  Created by apple on 2018/5/25.
//  Copyright © 2018年 锦. All rights reserved.
//

#import "CustomSlider.h"
#import "Masonry.h"

static int BTN_TAG = 453478;

@interface CustomSlider()

@property (nonatomic,strong) UIView *minView;

@property (nonatomic,strong) UIView *maxView;

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,strong) UIButton *dragButton;

@end

@implementation CustomSlider
{
    NSMutableArray *_pointBtnArr;
    NSMutableArray *_pointViewArr;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initSubViews];
}
- (BOOL)autoresizesSubviews {
    return YES;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    for (int i = 0 ; i< _pointBtnArr.count ; i++) {
        UIButton *btn =  _pointBtnArr[i];
        NSNumber *pointX  = _pointArray[i];
        CGFloat x = pointX.floatValue / _maximumValue * (self.frame.size.width - 10);
        CGPoint center = btn.center;
        center.x = x;
        btn.center = center;
//        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//            make.top.mas_offset(self.frame.size.height / 2 - 4);
//            make.width.mas_offset(4);
//            make.left.mas_equalTo(x * (self.frame.size.width - 10));
//            make.height.mas_offset(8);
//        }];
    }
//    self.maxView.frame = CGRectMake(0, self.frame.size.height / 2 - 2, self.frame.size.width, 4);
//    self.minView.frame = CGRectMake(0, self.frame.size.height / 2 - 2, self.frame.size.width / 2, 4);
    
}
- (void)setValue:(float)value {
    _value = value;
    if (_maximumValue <= 0) {
        return;
    }
    CGFloat x = value / _maximumValue ;
    if (x < 0 ) {
        x = 0;
    }
    if (x > 1) {
        x = 1;
    }
    CGFloat nowX = x * (self.frame.size.width - 10);
    
    CGPoint center =  _dragButton.center;
    center.x = nowX + 5;
    _dragButton.center = center;
    
}
- (void)setMaximumValue:(float)maximumValue {
    _maximumValue = maximumValue;
}
- (void)setMinimumValue:(float)minimumValue {
    _minimumValue = minimumValue;
}
- (void)setPointArray:(NSArray *)pointArray {
    for (int i = (int)_pointBtnArr.count - 1; i>= 0; i--) {
        UIButton *btn = _pointBtnArr[i];
        UIView *view = _pointViewArr[i];
        [view removeFromSuperview];
        view = nil;
        [_pointViewArr removeObject:view];
        [btn removeFromSuperview];
        [_pointBtnArr removeObject:btn];
        btn = nil;
        
    }
    
    _pointArray = pointArray;
    
    for (int i = 0 ; i<pointArray.count; i++) {
        NSNumber *pointX  = pointArray[i];
        CGFloat x = pointX.floatValue / _maximumValue ;
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x * (self.frame.size.width - 10), self.frame.size.height / 2 - 8, 20, 16)];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(8, 5, 4, 6)];
        [btn addSubview:view];
        view.layer.cornerRadius = 2;
        view.backgroundColor = [UIColor orangeColor];
        
        [self insertSubview:btn belowSubview:self.dragButton];
        btn.backgroundColor = [UIColor clearColor];
       // btn.layer.cornerRadius = 2;
//        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_offset(self.frame.size.height / 2 - 4);
//            make.width.mas_offset(4);
//            make.left.mas_equalTo(x * (self.frame.size.width - 10));
//            make.height.mas_offset(8);
//        }];
        [_pointBtnArr addObject:btn];
        [_pointViewArr addObject:view];
        btn.tag = BTN_TAG + i;
        [btn addTarget:self action:@selector(pointBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)pointBtnClicked:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(CustomSliderPointClicked:WithCenterX:)]) {
        [self.delegate CustomSliderPointClicked:(btn.tag - BTN_TAG) WithCenterX:(btn.center.x + 100)];
    }
}
- (void)initSubViews {
    
    _pointViewArr = [NSMutableArray array];
    _pointBtnArr = [NSMutableArray array];
    self.backgroundColor = [UIColor clearColor];
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.height / 2 - 5, self.frame.size.height / 2 - 5, 10, 10)];
    self.imageView.backgroundColor = [UIColor blueColor];
    self.imageView.layer.cornerRadius = 5;
    
    _dragButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
    _dragButton.backgroundColor = [UIColor clearColor];
    [_dragButton addSubview:self.imageView];
    
    [self addSubview:_dragButton];
    
   
    self.maxView = [[UIView alloc]init];//WithFrame:CGRectMake(0, self.frame.size.height / 2 - 2, self.frame.size.width, 4)];
    self.maxView.backgroundColor = [UIColor grayColor];
    
    self.minView = [[UIView alloc]init];//WithFrame:CGRectMake(0, self.frame.size.height / 2 - 2, self.frame.size.width, 4)];
    self.minView.backgroundColor = [UIColor redColor];
    
    
    
    [self insertSubview:self.maxView belowSubview:self.dragButton];
    [self insertSubview:self.minView belowSubview:self.dragButton];
    
    [self.maxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(self.frame.size.height / 2 - 2);
        make.right.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.height.mas_offset(4);
    }];
    [self.minView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(self.frame.size.height / 2 - 2);
        make.right.mas_equalTo(self.imageView.mas_left).offset(3);
        make.left.mas_equalTo(self);
        make.height.mas_offset(4);
    }];
    
    self.maxView.layer.cornerRadius = 2;
    self.minView.layer.cornerRadius = 2;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHandler:)];
    [_dragButton addGestureRecognizer:pan];
    UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHandler:)];
    [_imageView addGestureRecognizer:pan1];
    _imageView.userInteractionEnabled = YES;
    [_dragButton addTarget:self action:@selector(nothing) forControlEvents:UIControlEventTouchUpInside];
    
    
}
- (void)nothing {
    NSLog(@"nothing");
}

- (void)panHandler:(UIPanGestureRecognizer *)pan {
    CGPoint point = [pan locationInView:self];
    CGFloat x = point.x;
    if (x < 5) {
        x = 5;
    }
    if (x > self.frame.size.width - 5) {
        x = self.frame.size.width - 5;
    }
    
    
    CGPoint center =  _dragButton.center;
    center.x = x;
    _dragButton.center = center;
    _value = (x - 5) / (self.frame.size.width - 10) * _maximumValue;
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        _dragX = point.x + 100;
        if (_dragX < 100 ) {
            _dragX = 100;
        }
        if (_dragX >= self.frame.size.width + 100) {
            _dragX = self.frame.size.width + 100;
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(CustomSliderDraging:)]) {
            [self.delegate CustomSliderDraging:self];
        }
    }
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateFailed){
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(CustomSliderDragingEnd:)]) {
            [self.delegate CustomSliderDragingEnd:self];
        }
        
    }
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
